package com.ggj2013.hbh;

/**
 * @author kristian
 */
 
import com.eclecticdesignstudio.motion.Actuate;
import com.ggj2013.hbh.utils.Tools;
import com.ggj2013.hbh.utils.HitBox;
import nme.display.Shape;
import nme.display.MovieClip;
import nme.events.Event;
import nme.Lib;
import nme.display.Bitmap;


class SanityBar extends MovieClip {

	private var _bgBar:Bitmap;
	private var _totalHealthBar:Bitmap;
	private var _currentHealthBar:Bitmap;
	private var _maskingShape:Shape;
	
	private var _gaugeNum:Int;
	private var _gaugeBgs:Array<Dynamic>;
	
	private var _maxV:Float;
	private var _minV:Float;
	private var _currV:Float;
	
	private var _needleContainer:MovieClip;
	private var _needleBitmap:Bitmap;
	
	private static var BAR_XPOS:Int = -85;
	private static var BAR_YPOS:Int = -70;
	private static var NEEDLE_X:Int = -85;
	private static var NEEDLE_Y:Int = -70;
	
	public function new( l_maxV:Float, l_minV:Float, l_startV:Float ) {
		super();
		
		_maxV = l_maxV;
		_minV = l_minV;
		
		_gaugeNum = 0;
		
		//Add Background Bar
		_gaugeBgs = new Array<Dynamic>();
		
		var l_gauge:Bitmap = Tools.loadBitmap( "gauge0" );
		l_gauge.x = BAR_XPOS;
		l_gauge.y = BAR_YPOS;
		l_gauge.scaleX = l_gauge.scaleY = 2.0;
		addChild( l_gauge );
		_gaugeBgs.push( l_gauge );
		l_gauge.alpha = 0;
		
		l_gauge = Tools.loadBitmap( "gauge1" );
		l_gauge.x = BAR_XPOS;
		l_gauge.y = BAR_YPOS;
		l_gauge.scaleX = l_gauge.scaleY = 2.0;
		addChild( l_gauge );
		_gaugeBgs.push( l_gauge );
		l_gauge.alpha = 0;
		
		l_gauge = Tools.loadBitmap( "gauge2" );
		l_gauge.x = BAR_XPOS;
		l_gauge.y = BAR_YPOS;
		l_gauge.scaleX = l_gauge.scaleY = 2.0;
		addChild( l_gauge );
		_gaugeBgs.push( l_gauge );
		l_gauge.alpha = 0;
		
		l_gauge = Tools.loadBitmap( "gauge3" );
		l_gauge.x = BAR_XPOS;
		l_gauge.y = BAR_YPOS;
		l_gauge.scaleX = l_gauge.scaleY = 2.0;
		addChild( l_gauge );
		_gaugeBgs.push( l_gauge );
		l_gauge.alpha = 0;
		
		_gaugeBgs[ _gaugeNum ].alpha = 1;
		
		
		_needleBitmap = Tools.loadBitmap( "needle" );
		_needleBitmap.scaleX = _needleBitmap.scaleY = 2.0;
		_needleBitmap.x = -_needleBitmap.width / 2;// + 1;
		_needleBitmap.y = -3 * _needleBitmap.height / 4 - 6;
		//_needleBitmap.visible = false;
		
		/*var l_hitBox:HitBox = new HitBox( 2, 2, 0xff0000 );
		l_hitBox.x -= l_hitBox.width / 2;
		l_hitBox.y -= l_hitBox.height / 2;*/
		
		_needleContainer = new MovieClip();
		_needleContainer.addChild( _needleBitmap );
		//_needleContainer.addChild( l_hitBox );
		addChild( _needleContainer );
		_needleContainer.y += 19;
		_needleContainer.rotation = -150;
		
		//Add Depleted Health Bar
		/*_totalHealthBar = Tools.loadBitmap("red_bar");
		_totalHealthBar.x = (_bgBar.width - _totalHealthBar.width) / 2 ;
		_totalHealthBar.y = (_bgBar.height - _totalHealthBar.height) / 2;
		addChild( _totalHealthBar );
		
		//Add Current Health Bar
		_currentHealthBar = Tools.loadBitmap("green_bar");
		_currentHealthBar.x = (_bgBar.width - _currentHealthBar.width) / 2 ;
		_currentHealthBar.y = (_bgBar.height - _currentHealthBar.height) / 2;
		addChild( _currentHealthBar );
		
		//Add Mask for Current Health Bar
		_maskingShape = new Shape();
		addChild( _maskingShape );
		
		//Apply Mask to Health Bar
		_currentHealthBar.mask = _maskingShape;
		_maskingShape.x = _currentHealthBar.x;
		_maskingShape.y = _currentHealthBar.y;
		_maskingShape.graphics.beginFill(0xFFFFFF);
		_maskingShape.graphics.drawRect(0,0,_currentHealthBar.width,_currentHealthBar.height);
		_maskingShape.graphics.endFill();*/
		
		update( l_startV );
	}
	
	public function update( l_currV:Float ):Void {
		_currV = l_currV;
		
		var l_ratio:Float = _currV / _maxV;
		var l_newGauge:Int = -1;
		
		if ( l_ratio >= .75 )
		{
			l_newGauge = 0;
		}
		else if ( l_ratio >= .5 )
		{
			l_newGauge = 1;
		}
		else if ( l_ratio >= .25 )
		{
			l_newGauge = 2;
		}
		else
		{
			l_newGauge = 3;
		}
		
		if ( l_newGauge != _gaugeNum )
		{
			// TODO: tween this
			Actuate.tween(_gaugeBgs[ l_newGauge ], 4, { alpha: 1 } );
			Actuate.tween(_gaugeBgs[ _gaugeNum ], 4, { alpha: 0 } );
			//_gaugeBgs[ l_newGauge ].visible = true;
			//_gaugeBgs[ _gaugeNum ].visible = false;
			_gaugeNum = l_newGauge;
		}
		
		var l_rotation:Float = 0;
		
		//Current Health is within min/max values
		if ( _currV < _maxV && _currV > _minV )
		{
			l_rotation = l_ratio * 300 - 150;
		}
		//Current Health is less than minimum, set to 0
		if( _currV <= _minV )
		{
			l_rotation = -150;
		}
		//Current Health is greater than maximum, set to 1
		if( _currV >= _maxV )
		{
			l_rotation = 150;
		}
		
		//trace("ratio");
		//trace(l_ratio);
		_needleContainer.rotation = l_rotation;
		//trace("rotation");
		//trace(_needleContainer.rotation);
	}
}